package org.ramoths.netcom.sessions;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class SuiviFlux extends ReentrantLock
{
	private static final long serialVersionUID = 1558784343191710228L;
	private final Condition conditionFinEnvoi;
	private boolean fin;
	private Exception exception;
	
	SuiviFlux()
	{
		this.conditionFinEnvoi = super.newCondition();
		this.fin = false;
		this.exception = null;
	}
	
	void setFinFlux(Exception exception)
	{
		lock();
		try
		{
			fin = true;
			this.exception = exception;
			conditionFinEnvoi.signalAll();
		}
		finally
		{
			unlock();
		}
	}
	
	public void attenteFin() throws InterruptedException
	{
		lock();
		try
		{
			while (!fin)
				conditionFinEnvoi.await();
		}
		finally
		{
			unlock();
		}
	}
	
	public boolean isFin()
	{
		lock();
		try
		{
			return fin;
		}
		finally
		{
			unlock();
		}
	}
	
	public Exception getException()
	{
		lock();
		try
		{
			return exception;
		}
		finally
		{
			unlock();
		}
	}
}
